using TMPro;
using UnityEngine;

public class RelicViewExText : MonoBehaviour
{
    [Header("手动拖入引用")]
    [SerializeField] private RelicView relicView;  // 手动拖入 RelicView
    [SerializeField] private TMP_Text priceText;   // 手动拖入价格文本
    [SerializeField] private TMP_Text titleText;   // 手动拖入标题文本

    private Relic relic;

    private void Awake()
    {
        if (relicView == null)
            Debug.LogError($"{name}: relicView 未绑定！");
        if (titleText == null)
            Debug.LogError($"{name}: titleText 未绑定！");
        if (priceText == null)
            Debug.LogError($"{name}: priceText 未绑定！");

        // 默认隐藏
        if (titleText != null) titleText.gameObject.SetActive(false);
        if (priceText != null) priceText.gameObject.SetActive(false);
    }

    public void SetupText(bool showTitle = true, bool showPrice = true)
    {
        // 延迟获取 relic，保证它已经在 RelicView.Setup 中赋值
        if (relic == null && relicView != null)
            relic = relicView.relic;

        if (relic == null)
        {
            Debug.LogWarning($"{name}: relic 还没赋值！");
            return;
        }

        // 标题
        if (showTitle && titleText != null)
        {
            titleText.text = relic.ShowTitle;
            titleText.gameObject.SetActive(true);
            Debug.Log($"{name}: 设置标题 {relic.Title}");
        }

        // 价格
        if (showPrice && priceText != null)
        {
            if (StoreSystem.ItemTotalPriceDict.TryGetValue(relic.ShowTitle, out int itemPrice))
            {
                priceText.text = itemPrice.ToString();
                priceText.gameObject.SetActive(true);
                Debug.Log($"{name}: 设置价格 {itemPrice}");
            }
            else
            {
                Debug.LogWarning($"{name}: 没找到 {relic.Title} 的价格");
            }
        }
    }
}
